package com.example.crossborderecommerce.config;

import com.example.crossborderecommerce.config.interceptor.JwtInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.Resource;
import java.io.File;

@Configuration
public class HttpInterceptorConfig extends WebMvcConfigurationSupport {
    @Resource
    private JwtInterceptor jwtInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/api/**") //拦截所有请求，判断token是否合法决定是否需要登录
                .excludePathPatterns("/api/admin/login", "/api/admin/register", "/api/common/download");
    }


    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.addPathPrefix("/api", c -> c.isAnnotationPresent(RestController.class));
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        /**
         * 部署时
         */
        registry.addResourceHandler("/oj/**").addResourceLocations("file:" + System.getProperty("user.dir")+ File.separator + "upload");
        /**
         * 开发时
         */
//        registry.addResourceHandler("/oj/**").addResourceLocations("file:OJ-backend/upload");
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        super.addResourceHandlers(registry);
    }

    @Override
    protected void addViewControllers(ViewControllerRegistry registry) {
        // 访问 / 转向 index.html 页面
        registry.addViewController("/").setViewName("forward:index.html");
        super.addViewControllers(registry);
    }

}
